package com.yiqi.zhiyuan.feature.home.presenter;

import android.text.TextUtils;

import com.suozhang.framework.component.http.ApiError;
import com.suozhang.framework.component.http.ex.ApiException;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.zhiyuan.entity.dto.LocationDTO;
import com.yiqi.zhiyuan.entity.dto.TaskDTO;
import com.yiqi.zhiyuan.entity.vo.CarVO;
import com.yiqi.zhiyuan.entity.vo.HomeVO;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.home.contract.HomeContract;
import com.yiqi.zhiyuan.feature.home.dependencies.home.DaggerHomePresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HomePresenter implements HomeContract.Presenter {

    private HomeContract.View mView;

    @Inject
    HomeContract.Model mModel;


    public HomePresenter(HomeContract.View mView) {
        this.mView = mView;
        DaggerHomePresenterComponent.create().inject(this);
    }


    @Override
    public void selectDriverHomeInfo() {
        Logger.e("selectDriverHomeInfo---------------->");
        mModel.dosSelectDriverHomeInfo()
                .compose(mView.<HomeVO>bindToLife())
                .subscribe(new Observer<HomeVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(HomeVO homeVo) {
//                        mView.dismissLoading();
                        mView.showHomeInfoSuccess(homeVo);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.dismissLoading();
                        mView.showHomeInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
//                        mView.dismissLoading();
                    }
                });

    }

    @Override
    public void selectCarList() {
        mModel.doSelectCarList()
                .compose(mView.<List<CarVO>>bindToLife())
                .subscribe(new Observer<List<CarVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<CarVO> carVOS) {
                        mView.dismissLoading();
                        if (carVOS != null && !carVOS.isEmpty()) {
                            mView.showCarListSuccess(carVOS);
                        } else {
                            mView.showSelectCarListEmpty();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showSelectCarListError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void orderQuickSetup(Integer driverId, int type) {
        mModel.doOrderQuickSetup(driverId, type)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String homeVo) {
                        mView.dismissLoading();
                        mView.showOrderQuickSetupSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuickSetupError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void order(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doOrder(dto)
                .compose(mView.<OrderVO>bindToLife())
                .subscribe(new Observer<OrderVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(OrderVO homeVo) {
                        mView.dismissLoading();
                        mView.showOrderSuccess(homeVo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        ApiException exception = (ApiException) e;
                        ApiError error = ApiError.codeOf(exception.code());
                        if (error == ApiError.CODE_OVERRUN) {
                            mView.showOrderError();
                        } else {
                            mView.showOrderError(e.getMessage());
                        }
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void arriveAtLocation(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doArriveAtLocation(dto)
                .compose(mView.<OrderVO>bindToLife())
                .subscribe(new Observer<OrderVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(OrderVO homeVo) {
                        mView.dismissLoading();
                        mView.showArriveAtLocationSuccess(homeVo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void selectMissedOrderList(double lng,double lat) {
        mModel.doSelectMissedOrderList(new LocationDTO(lng,lat))
                .compose(mView.<List<OrderVO>>bindToLife())
                .subscribe(new Observer<List<OrderVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<OrderVO> homeVo) {
                        if (homeVo != null && homeVo.size() > 0) {
                            mView.showMissedOrderListSuccess(homeVo);
                        } else {
                              mView.showMissedOrderListEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                        mView.showMissedOrderListError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void start() {

    }
}
